'''
Created on Feb 8, 2012

@author: oabalbin
'''
import os
import subprocess

JOB_ERROR=1
JOB_SUCCESS=0

def project(rootdir,name,sample_dict,build):
    '''
    '''
    
    os.chdir(rootdir)
    args=['mkdir',name]
    retcode = subprocess.call(args)
    os.chdir(os.path.join(rootdir,name))
        
    args=['vtools','init',name]
    retcode=subprocess.call(args)

    if retcode!=0:
        return JOB_ERROR
    cmd=['vtools','import',]
    for sp,file in sample_dict.iteritems():
        args=cmd+[file]+['--sample_name',sp]+['--build',build]
        if retcode != 0:
            raise JOB_ERROR
        else:
            print args
            retcode=subprocess.call(args)
    if retcode!=0:
        return JOB_ERROR
    return JOB_SUCCESS

def load_gene_annotations(rootdir,name):
    '''
    This will load the gene annotation
    '''
    os.chdir(os.path.join(rootdir,name))
    annolist=['refGene','ccdsGene']
    cmd=['vtools','use']
    retcode=0
    for ga in annolist:
        args=cmd+[ga]
        if retcode != 0:
            raise JOB_ERROR
        else:
            print args
            retcode=subprocess.call(args)

    if retcode!=0:
        return JOB_ERROR
    return JOB_SUCCESS

def load_variant_annoations(rootdir,name):
    '''
    'evs'=exome variant server (2500 exomes from the NHLBI),
    'evs_5400=exome variant server (5400 exomes from the NHLBI)'
    'dbSNP',
    'dbNSFP'=non-synonymous variants of CCDS genes,
    'thousandGenomes=1000 genomes'
    '''
    
    os.chdir(os.path.join(rootdir,name))
    annolist=['evs','evs_5400','dbSNP','dbNSFP','thousandGenomes']
    cmd=['vtools','use']
    retcode=0
    for ga in annolist:
        args=cmd+[ga]
        if retcode != 0:
            raise JOB_ERROR
        else:
            print args
            retcode=subprocess.call(args)

    if retcode!=0:
        return JOB_ERROR
    return JOB_SUCCESS
    

def load_functional_annoations(rootdir,name):
    '''
    two main annotation= kegg pathways
    and cancer genome census
    they both required to have loaded refGene or other gene annotation first
    '''
    
    os.chdir(os.path.join(rootdir,name))
    annolist={'CancerGeneCensus':'refGene.name2','keggPathway':'ccdsGene.name'} # test if you can link kegg pathways using the refGene.name2
    retcode=0
    for ga,linkby in annolist.iteritems():
        args=['vtools','use',ga,'--linked_by',linkby] # refGene.name2 is the hugo symbol
        if retcode != 0:
            raise JOB_ERROR
        else:
            print args
            retcode=subprocess.call(args)

    if retcode!=0:
        return JOB_ERROR
    return JOB_SUCCESS


    
'''
samples_dict={'fmpn63':'/exds/users/oabalbin/projects/fmpn/data/gatk_calls/FMPN63.gatk_snps.b.filtered.model.vcf',
'fmpn51':'/exds/users/oabalbin/projects/fmpn/data/gatk_calls/FMPN51.gatk_snps.b.filtered.model.vcf',
'fmpn54','/exds/users/oabalbin/projects/fmpn/data/gatk_calls/FMPN54.gatk_snps.b.filtered.model.vcf',
'fmpn52','/exds/users/oabalbin/projects/fmpn/data/gatk_calls/FMPN52.gatk_snps.b.filtered.model.vcf',
'fmpn53','/exds/users/oabalbin/projects/fmpn/data/gatk_calls/FMPN53.gatk_snps.b.filtered.model.vcf',
'fmpn65','/exds/users/oabalbin/projects/fmpn/data/gatk_calls/FMPN65.gatk_snps.b.filtered.model.vcf',
'fmpn62','/exds/users/oabalbin/projects/fmpn/data/gatk_calls/FMPN62.gatk_snps.b.filtered.model.vcf'}
'''
samples_dict={'sam':'/exds/projects/rnaseq/tmp/hiseqtest/jolib/var.flt.vcf',
              'vars':'/exds/projects/rnaseq/runs/hiseqtest/jolib/var.varscan.flt.vcf'}
build='hg19'
rootdir='/data/projects'
#name='familial_fmpn'
name='vtools_test'
if __name__ == '__main__': 
    project(rootdir,name,samples_dict,build)
